package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;
import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class InGameMenu extends State {

	private final int MENU_ITEM_RESUME = 0;
	private final int MENU_ITEM_OPTIONS = MENU_ITEM_RESUME + 1;
	private final int MENU_ITEM_HELP = MENU_ITEM_OPTIONS + 1;
	private final int MENU_ITEM_QUIT = MENU_ITEM_HELP + 1;
	private final int MENU_ITEM_EXIT = MENU_ITEM_QUIT + 1;

	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private int sNumMenuItem = 5;
	private Menu m_menu;
	private Texture m_texture = new Texture();
	private int[] m_iMenuItemTex = new int[sNumMenuItem];
	private int[] m_iMenutemSelectedTex = new int[sNumMenuItem];
	
	private float m_fMenuItemWidth;
	private float m_fMenuItemHeight;

	private int selected = MENU_ITEM_RESUME;


	@Override
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		m_fMenuItemWidth = Device.MENU_BUTTON_WIDTH;
		m_fMenuItemHeight = Device.MENU_BUTTON_HEIGHT;

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();

		float w = m_fSrcWidth / 2;
		float h = m_fSrcHeight / 2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();

		// Menu
		loadMenuTexture();
		m_menu = new Menu(gl, sNumMenuItem, (int) m_fMenuItemWidth,
				(int) m_fMenuItemHeight);		
		m_menu.SetEnableScaleItem(false);
		
		for (int i = 0; i < sNumMenuItem; ++i) {
			m_menu.AddMenuItem(m_iMenuItemTex[i], m_iMenutemSelectedTex[i]);
		}
	}

	@Override
	public void Update(GamePlay gamePlay) {
		
		super.Update(gamePlay);
		
		// add code here
		
	}

	public void Render() {
		gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		m_menu.Render();
	}

	@Override
	public void Destroy() {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();

		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		m_texture = null;
		m_menu = null;
		m_iMenutemSelectedTex = null;
		m_iMenuItemTex = null;
	}

	private void loadMenuTexture() {

		// Start Game
		m_iMenuItemTex[MENU_ITEM_RESUME] = m_texture
				.initTexture(R.drawable.mnu_mmn_resume);
		m_iMenutemSelectedTex[MENU_ITEM_RESUME] = m_texture
				.initTexture(R.drawable.mnu_mmn_resume_select);


		// Options
		m_iMenuItemTex[MENU_ITEM_OPTIONS] = m_texture
				.initTexture(R.drawable.mnu_mmn_options);
		m_iMenutemSelectedTex[MENU_ITEM_OPTIONS] = m_texture
				.initTexture(R.drawable.mnu_mmn_options_select);

		// Help
		m_iMenuItemTex[MENU_ITEM_HELP] = m_texture
				.initTexture(R.drawable.mnu_mmn_help);
		m_iMenutemSelectedTex[MENU_ITEM_HELP] = m_texture
				.initTexture(R.drawable.mnu_mmn_help_select);

		// Quit to main menu
		m_iMenuItemTex[MENU_ITEM_QUIT] = m_texture
				.initTexture(R.drawable.mnu_mmn_quit);
		m_iMenutemSelectedTex[MENU_ITEM_QUIT] = m_texture
				.initTexture(R.drawable.mnu_mmn_quit_select);

		// Exit program
		m_iMenuItemTex[MENU_ITEM_EXIT] = m_texture
				.initTexture(R.drawable.mnu_mmn_exit);
		m_iMenutemSelectedTex[MENU_ITEM_EXIT] = m_texture
				.initTexture(R.drawable.mnu_mmn_exit_select);

	}

	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if (KeyBoard.IsKeyPressed(DK_PAD_DOWN)) {
			++selected;
			if (selected > MENU_ITEM_EXIT)
				selected = MENU_ITEM_RESUME;

			m_menu.SetSelectedMenuItem(selected);
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		} else if (KeyBoard.IsKeyPressed(DK_PAD_UP)) {
			--selected;
			if (selected < MENU_ITEM_RESUME)
				selected = MENU_ITEM_EXIT;

			m_menu.SetSelectedMenuItem(selected);
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		} else if (KeyBoard.IsKeyPressed(DK_PAD_MIDDLE)) {
			switch (selected) {
			case MENU_ITEM_RESUME:
				if(!gamePlay.ResumeState())
					gamePlay.SwitchState(GameState.MainMenu);
				break;
			case MENU_ITEM_OPTIONS:
				gamePlay.SwitchSubState(GameState.Options);
				break;
			case MENU_ITEM_HELP:
				gamePlay.SwitchSubState(GameState.Help);
				break;
			case MENU_ITEM_QUIT:
				gamePlay.SwitchSubState(GameState.QuitMission);
				break;
			case MENU_ITEM_EXIT:
				gamePlay.SwitchSubState(GameState.Exit);
				break;
			}
		}
	}
}
